﻿using HPIT.RentHouse.Admin.App_Start;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Results;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    public class LoginController : Controller
    {

        IAdminUsersService adminUserService;
        public LoginController(IAdminUsersService adminUserService)
        {
            this.adminUserService = adminUserService;
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>返回JSON数据</returns>
        [HttpPost]
        [LoggingActionFilter]
        public JsonResult Login(LoginDTO dto)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            //处理跨域请求
            Response.Headers.Add("Access-Control-Allow-Origin", "*");
            if (adminUserService.Login(dto))
            {
                dictionary.Add("msg", "success");
                dictionary.Add("code",0);
            }
            else {
                dictionary.Add("msg", "error");
                dictionary.Add("code", -1);
            }
            return Json(dictionary,JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>返回JSON数据</returns>
        [HttpPost]
        [LoggingActionFilter]
        public JsonResult Regist(LoginDTO dto)
        {
         
            //处理跨域请求
            Response.Headers.Add("Access-Control-Allow-Origin", "*");
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            //随机生成盐
            dto.Salt = CommonHelper.CreateVerifyCode(6);
            dto.Password = CommonHelper.CalcMD5(dto.Password + dto.Salt);
           int i =  adminUserService.Add(dto);
            if (i > 0)
            {
                dictionary.Add("msg", "success");
                dictionary.Add("code", i);
            }
            else {
                dictionary.Add("msg", "error");
                dictionary.Add("code", i);
            }
            return Json(dictionary, JsonRequestBehavior.AllowGet);
        }
    }
}